#include "esp_log.h"
#include "lv_app_main.h"
#include "lvgl.h"
#include "ui.h"
#include "hlw8032.h"
#include "aht20_hal.h"
#include "pcf8563.h"
#include "led_hal.h"
#include "ch423.h"
#include "esp_now_hal.h"
#include "wifi_hal.h"
#include "protocol_examples_common.h"
#include "example_common_private.h"

static const char *TAG = "lv_app_main";

/* define lv_timer start */
static lv_timer_t *updata_task1 = NULL;
static lv_app_main_language language_state = {0, 0};
static lv_timer_count time_count = {0, 0, 0};
static char str_dis[30] = "mqtt push ok!\nCHANNEL >> 5";
/* define lv_timer end */

/* define brightness start */
static uint8_t set_brightness_last, set_brightness_last_now;
/* define brightness end */

/* define time count start */
static uint8_t Time_row, Time_row1, Msg_row;
/* define time count end */

/**
 * @brief lv_set_english_font
 *
 */
static void lv_set_english_font(void)
{
    /* home */
    lv_label_set_text(ui_WeatherLabel, "Sunny 26.3C°");
    lv_obj_set_style_text_font(ui_WeatherLabel, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_HomeDateLabel, "Tuesday, April 20th");
    lv_obj_set_style_text_font(ui_HomeDateLabel, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    /* ctrl */
    /* living room */
    lv_label_set_text(ui_Living_Light_Label, "Light");
    lv_obj_set_style_text_font(ui_Living_Light_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Living_Fan_Label, "Fan");
    lv_obj_set_style_text_font(ui_Living_Fan_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Air_C_Label, "Air conditioning");
    lv_obj_set_style_text_font(ui_Air_C_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Living_TV_Label, "Smart TV");
    lv_obj_set_style_text_font(ui_Living_TV_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    // lv_label_set_text(ui_Living_Router_Label, "Router");
    // lv_obj_set_style_text_font(ui_Living_Router_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Living_Air_P_Label, "Air purifier");
    lv_obj_set_style_text_font(ui_Living_Air_P_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    /* bed room */
    lv_label_set_text(ui_Bed_Light_Label, "Light");
    lv_obj_set_style_text_font(ui_Bed_Light_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Bed_Fan_Label, "Fan");
    lv_obj_set_style_text_font(ui_Bed_Fan_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Bed_A_C_Label, "Air conditioning");
    lv_obj_set_style_text_font(ui_Bed_A_C_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    // lv_label_set_text(ui_Bed_A_P_Label, "Air purifier");
    // lv_obj_set_style_text_font(ui_Bed_A_P_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);

    /* power */
    lv_label_set_text(ui_E_V_Label, "Voltage");
    lv_obj_set_style_text_font(ui_E_V_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_E_C_Label, "Current");
    lv_obj_set_style_text_font(ui_E_C_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_AC_P_Label, "ac Power");
    lv_obj_set_style_text_font(ui_AC_P_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_AP_P_Label, "ap Power");
    lv_obj_set_style_text_font(ui_AP_P_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_F_Label, "Factor");
    lv_obj_set_style_text_font(ui_F_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_PF_C_Label, "PF Count");
    lv_obj_set_style_text_font(ui_PF_C_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_One_Pulses_Label, "One pulses");
    lv_obj_set_style_text_font(ui_One_Pulses_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_Used_Label, "Used");
    lv_obj_set_style_text_font(ui_Used_Label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_ElectricalOpenNumLabel, "ElectricalOpenNum          ---                                           0");
    lv_obj_set_style_text_font(ui_ElectricalOpenNumLabel, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);

    /* settings */
    // lv_label_set_text(ui_SettingsBlueLabel, "Bluetooth");
    // lv_obj_set_style_text_font(ui_SettingsBlueLabel, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_SettingsWifiLabel, "WLAN");
    lv_obj_set_style_text_font(ui_SettingsWifiLabel, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_SettingsAboutLabel, "About");
    lv_obj_set_style_text_font(ui_SettingsAboutLabel, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_SettingsBrightnessLabel, "Bright");
    lv_obj_set_style_text_font(ui_SettingsBrightnessLabel, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_SettingsLanguageLabel, "Language");
    lv_obj_set_style_text_font(ui_SettingsLanguageLabel, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    /* about */
    lv_label_set_text(ui_AboutNameSwLabel, "Device name\nModel name\nModel version");
    lv_obj_set_style_text_font(ui_AboutNameSwLabel, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_AboutChipSwLabel, "Processor\nRAM\nFlash\nResolution");
    lv_obj_set_style_text_font(ui_AboutChipSwLabel, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(ui_AboutRunningTimeLabel, "Running time");
    lv_obj_set_style_text_font(ui_AboutRunningTimeLabel, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);
    /* wifi */
    lv_label_set_text(ui_Wifi_Ensure_Button_Label, "CONNECT");
    lv_obj_set_style_text_font(ui_Wifi_Ensure_Button_Label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    /* time */
    lv_label_set_text(ui_SettingsTime, "Time");
    lv_obj_set_style_text_font(ui_SettingsTime, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
    /* language */
    lv_label_set_text(ui_LanguageLabel, "English                  Chinese");
    lv_obj_set_style_text_font(ui_LanguageLabel, &lv_font_montserrat_24, LV_PART_MAIN | LV_STATE_DEFAULT);
}

/**
 * @brief lv_set_user_font
 *
 */
static void lv_set_user_font(void)
{
    /* home */
    lv_label_set_text(ui_WeatherLabel, "晴  26.3C°");
    lv_obj_set_style_text_font(ui_WeatherLabel, &msyh_18_Font, 0);
    lv_label_set_text(ui_HomeDateLabel, "星期四 1月21日");
    lv_obj_set_style_text_font(ui_HomeDateLabel, &msyh_18_Font, 0);

    /* ctrl */
    /* living room */
    lv_label_set_text(ui_Living_Light_Label, "灯");
    lv_obj_set_style_text_font(ui_Living_Light_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Living_Fan_Label, "风扇");
    lv_obj_set_style_text_font(ui_Living_Fan_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Air_C_Label, "空调");
    lv_obj_set_style_text_font(ui_Air_C_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Living_TV_Label, "智能电视");
    lv_obj_set_style_text_font(ui_Living_TV_Label, &msyh_18_Font, 0);
    // lv_label_set_text(ui_Living_Router_Label, "路由器");
    // lv_obj_set_style_text_font(ui_Living_Router_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Living_Air_P_Label, "空气净化器");
    lv_obj_set_style_text_font(ui_Living_Air_P_Label, &msyh_18_Font, 0);
    /* bed room */
    lv_label_set_text(ui_Bed_Light_Label, "灯");
    lv_obj_set_style_text_font(ui_Bed_Light_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Bed_Fan_Label, "风扇");
    lv_obj_set_style_text_font(ui_Bed_Fan_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Bed_A_C_Label, "空调");
    lv_obj_set_style_text_font(ui_Bed_A_C_Label, &msyh_18_Font, 0);
    // lv_label_set_text(ui_Bed_A_P_Label, "空气净化器");
    // lv_obj_set_style_text_font(ui_Bed_A_P_Label, &msyh_18_Font, 0);

    /* power */
    lv_label_set_text(ui_E_V_Label, "有效电压");
    lv_obj_set_style_text_font(ui_E_V_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_E_C_Label, "有效电流");
    lv_obj_set_style_text_font(ui_E_C_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_AC_P_Label, "有功功率");
    lv_obj_set_style_text_font(ui_AC_P_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_AP_P_Label, "视在功率");
    lv_obj_set_style_text_font(ui_AP_P_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_F_Label, "功率因数");
    lv_obj_set_style_text_font(ui_F_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_PF_C_Label, "上电脉冲数");
    lv_obj_set_style_text_font(ui_PF_C_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_One_Pulses_Label, "1度电需脉冲");
    lv_obj_set_style_text_font(ui_One_Pulses_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Used_Label, "已用电量");
    lv_obj_set_style_text_font(ui_Used_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_ElectricalOpenNumLabel, "开启用电器数量             ---                                           0");
    lv_obj_set_style_text_font(ui_ElectricalOpenNumLabel, &msyh_18_Font, 0);

    /* settings */
    // lv_label_set_text(ui_SettingsBlueLabel, "蓝牙");
    // lv_obj_set_style_text_font(ui_SettingsBlueLabel, &msyh_18_Font, 0);
    lv_label_set_text(ui_SettingsBrightnessLabel, "屏幕亮度");
    lv_obj_set_style_text_font(ui_SettingsBrightnessLabel, &msyh_18_Font, 0);
    lv_label_set_text(ui_SettingsAboutLabel, "关于");
    lv_obj_set_style_text_font(ui_SettingsAboutLabel, &msyh_18_Font, 0);
    /* wifi */
    lv_label_set_text(ui_Wifi_Switch_Label, "WIFI 关");
    lv_obj_set_style_text_font(ui_Wifi_Switch_Label, &msyh_18_Font, 0);
    lv_label_set_text(ui_Wifi_Ensure_Button_Label, "连接");
    lv_obj_set_style_text_font(ui_Wifi_Ensure_Button_Label, &msyh_18_Font, 0);
    /* time */
    lv_label_set_text(ui_SettingsTime, "时间");
    lv_obj_set_style_text_font(ui_SettingsTime, &msyh_18_Font, 0);
    /* about */
    lv_label_set_text(ui_AboutNameSwLabel, "设备名称\n型号名称\n型号版本");
    lv_obj_set_style_text_font(ui_AboutNameSwLabel, &msyh_18_Font, 0);
    lv_label_set_text(ui_AboutChipSwLabel, "处理器\n运行内存\n存储\n屏幕");
    lv_obj_set_style_text_font(ui_AboutChipSwLabel, &msyh_18_Font, 0);
    lv_label_set_text(ui_AboutRunningTimeLabel, "开启时间");
    lv_obj_set_style_text_font(ui_AboutRunningTimeLabel, &msyh_18_Font, 0);
    /* language */
    lv_label_set_text(ui_SettingsLanguageLabel, "语言");
    lv_obj_set_style_text_font(ui_SettingsLanguageLabel, &msyh_18_Font, 0);
    lv_label_set_text(ui_LanguageLabel, "英文                      中文");
    lv_obj_set_style_text_font(ui_LanguageLabel, &msyh_18_Font, 0);
}

/**
 * @brief language_event_handler
 *
 * @param e
 */
static void language_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    if (code == LV_EVENT_VALUE_CHANGED)
    {
        lv_obj_has_state(obj, LV_STATE_CHECKED) ? (lv_set_user_font())
                                                : (lv_set_english_font());
        lv_obj_has_state(obj, LV_STATE_CHECKED) ? (language_state.CN = 1)
                                                : (language_state.CN = 0);
    }
}

/**
 * @brief connect_wifi_switch_event_handler
 *
 * @param e
 */
static void connect_wifi_switch_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    if (code == LV_EVENT_VALUE_CHANGED)
    {
        if (obj == ui_Wifi_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
                wifi_switch_set(1);
#if WIWI_AHL_DEBUG
                printf("open wifi.\r\n");
#endif
            }
            else
            {
                // example_wifi_sta_do_disconnect();
                // example_wifi_stop();
                wifi_switch_set(0);
#if WIWI_AHL_DEBUG
                printf("close wifi.[disconnect wifi]\r\n");
#endif
            }
        }
    }
}

/**
 * @brief connect_wifi_button_event_handler
 *
 * @param e
 */
static void connect_wifi_button_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        if (wifi_panel().authmode != WIFI_AUTH_OPEN)
        {
#if WIWI_AHL_DEBUG
            printf("wifi is connet.pleaase not retry option.\r\n");
#endif
        }
        else
        {
            if (wifi_switch_panel().wifi_status == 1)
            {
#if WIWI_AHL_DEBUG
                printf("wifi start connet.\r\n");
                printf("SSID:%s    PASSWORD:%s\r\n", lv_textarea_get_text(ui_SSID_TextArea), lv_textarea_get_text(ui_Password_TextArea));
#endif
                set_wifi_hal_conf(lv_textarea_get_text(ui_SSID_TextArea), lv_textarea_get_text(ui_Password_TextArea)); /* connect wifi */
                // example_wifi_connect();
            }
            else
            {
#if WIWI_AHL_DEBUG
                printf("please open wifi.\r\n");
#endif
            }
        }
    }
}

/**
 * @brief set_time_event_handler
 *
 * @param e
 */
static void set_time_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        uint8_t weekday = pcf8563_calculate_Weekday(lv_roller_get_selected(ui_Year_Roller) + 2020, lv_roller_get_selected(ui_Month_Roller) + 1, lv_roller_get_selected(ui_Day_Roller) + 1);
        printf("preset-> date:%d-%d-%d  time:%d:%d:%d  星期:%d\r\n",
               lv_roller_get_selected(ui_Year_Roller) + 2020,
               lv_roller_get_selected(ui_Month_Roller) + 1,
               lv_roller_get_selected(ui_Day_Roller) + 1,
               lv_roller_get_selected(ui_Hour_Roller) + 1,
               lv_roller_get_selected(ui_Min_Roller) + 1,
               lv_roller_get_selected(ui_Sec_Roller) + 1,
               weekday + 1);
        if (pcf8563_set_time(lv_roller_get_selected(ui_Sec_Roller) + 1,
                             lv_roller_get_selected(ui_Min_Roller) + 1,
                             lv_roller_get_selected(ui_Hour_Roller) + 1,
                             lv_roller_get_selected(ui_Day_Roller) + 1,
                             weekday,
                             lv_roller_get_selected(ui_Month_Roller) + 2,
                             lv_roller_get_selected(ui_Year_Roller) + 2020) == ESP_OK)
        {
            printf("setting time successfully.\r\n");
        }
    }
}

/**
 * @brief on_close_button_handler
 *
 * @param e
 */
static void on_close_button_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
        if (obj == ui_ON_ALL_Button)
        {
            for (uint8_t i = 0; i < 8; i++)
            {
                ch423_set_oc_level(i, 0); /* on all relay */
            }
        }
        else if (obj == ui_All_Close_Button)
        {
            for (uint8_t i = 0; i < 8; i++)
            {
                ch423_set_oc_level(i, 1); /* close all relay */
            }
        }
    }
}

/**
 * @brief relay_event_handler
 *
 * @param e
 */
static void relay_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    if (code == LV_EVENT_VALUE_CHANGED)
    {
        if (obj == ui_Living_Light_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Living_Light_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_0, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Living_Light_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_0, 1);
            }
        }
        else if (obj == ui_Living_Fan_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Living_Fan_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_1, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Living_Fan_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_1, 1);
            }
        }
        else if (obj == ui_Air_C_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Air_C_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_2, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Air_C_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_2, 1);
            }
        }
        else if (obj == ui_Living_TV_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Living_TV_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_3, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Living_TV_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_3, 1);
            }
        }
        else if (obj == ui_Living_Air_P_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Living_Air_P_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_4, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Living_Air_P_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_4, 1);
            }
        }
        else if (obj == ui_Bed_Light_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Bed_Light_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_5, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Bed_Light_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_5, 1);
            }
        }
        else if (obj == ui_Bed_Fan_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Bed_Fan_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_6, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Bed_Fan_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_6, 1);
            }
        }
        else if (obj == ui_Bed_A_C_Switch)
        {
            if (lv_obj_has_state(obj, LV_STATE_CHECKED) == 1)
            {
#if CH423_DEBUG
                printf("ui_Bed_A_C_Switch:1\r\n");
#endif
                ch423_set_oc_level(OC_L_7, 0);
            }
            else
            {
#if CH423_DEBUG
                printf("ui_Bed_A_C_Switch:0\r\n");
#endif
                ch423_set_oc_level(OC_L_7, 1);
            }
        }
    }
}

/**
 * @brief update_switch_ui_state
 *
 * @param data
 */
static void update_switch_ui_state(uint8_t data)
{
    if ((data & 0x01) == 0)                                         /* OC_L bit0 */
        lv_obj_add_state(ui_Living_Light_Switch, LV_STATE_CHECKED); /* bit0 -> open */
    else
        lv_obj_clear_state(ui_Living_Light_Switch, LV_STATE_CHECKED); /* bit0 -> close */

    if ((data & 0x02) == 0)                                       /* OC_L bit1 */
        lv_obj_add_state(ui_Living_Fan_Switch, LV_STATE_CHECKED); /* bit1 -> open */
    else
        lv_obj_clear_state(ui_Living_Fan_Switch, LV_STATE_CHECKED); /* bit1 -> close */

    if ((data & 0x04) == 0)                                  /* OC_L bit2 */
        lv_obj_add_state(ui_Air_C_Switch, LV_STATE_CHECKED); /* bit2 -> open */
    else
        lv_obj_clear_state(ui_Air_C_Switch, LV_STATE_CHECKED); /* bit2 -> close */

    if ((data & 0x08) == 0)                                      /* OC_L bit3 */
        lv_obj_add_state(ui_Living_TV_Switch, LV_STATE_CHECKED); /* bit3 -> open */
    else
        lv_obj_clear_state(ui_Living_TV_Switch, LV_STATE_CHECKED); /* bit3 -> close */

    if ((data & 0x10) == 0)                                         /* OC_L bit0 */
        lv_obj_add_state(ui_Living_Air_P_Switch, LV_STATE_CHECKED); /* bit0 -> open */
    else
        lv_obj_clear_state(ui_Living_Air_P_Switch, LV_STATE_CHECKED); /* bit0 -> close */

    if ((data & 0x20) == 0)                                      /* OC_L bit0 */
        lv_obj_add_state(ui_Bed_Light_Switch, LV_STATE_CHECKED); /* bit0 -> open */
    else
        lv_obj_clear_state(ui_Bed_Light_Switch, LV_STATE_CHECKED); /* bit0 -> close */

    if ((data & 0x40) == 0)                                    /* OC_L bit0 */
        lv_obj_add_state(ui_Bed_Fan_Switch, LV_STATE_CHECKED); /* bit0 -> open */
    else
        lv_obj_clear_state(ui_Bed_Fan_Switch, LV_STATE_CHECKED); /* bit0 -> close */

    if ((data & 0x80) == 0)                                    /* OC_L bit0 */
        lv_obj_add_state(ui_Bed_A_C_Switch, LV_STATE_CHECKED); /* bit0 -> open */
    else
        lv_obj_clear_state(ui_Bed_A_C_Switch, LV_STATE_CHECKED); /* bit0 -> close */
}

/**
 * @brief updata_task1_cb
 *
 * @param tmr lvgl timer struct
 */
static void updata_task1_cb(lv_timer_t *tmr)
{

    time_count.lv_time1 += 1;

    /* update RTC time start */
    if (time_count.lv_time1 % 5 == 0)
    {
        lv_label_set_text_fmt(ui_TimeLabel, "%02d:%02d", pcf8563_get_data(HOUR), pcf8563_get_data(MIN));
        lv_label_set_text_fmt(ui_HomeTimeLabel, "%02d : %02d", pcf8563_get_data(HOUR), pcf8563_get_data(MIN));
        lv_roller_set_selected(ui_TimeSecRoller, pcf8563_get_data(SEC) % 10, LV_ANIM_ON);
        lv_roller_set_selected(ui_TimeSecRoller1, pcf8563_get_data(SEC) / 10, LV_ANIM_ON);
    }
    /* update RTC time end */

    /* update hlw8032 screen data start */
    if (time_count.lv_time1 % 5 == 0)
    {
        lv_label_set_text_fmt(ui_E_V_Data_Label, "%.3f V", hlw8032_get_data(EFFECTIVE_VOLTAGE));
        lv_label_set_text_fmt(ui_E_C_Data_Label, "%.3f A", hlw8032_get_data(EFFECTIVE_CURRENT));
        lv_label_set_text_fmt(ui_AC_P_Data_Label, "%.3f W", hlw8032_get_data(ACTIVE_POWER));
        lv_label_set_text_fmt(ui_AP_P_Data_Label, "%.3f W", hlw8032_get_data(APPARENT_POWER));
        lv_label_set_text_fmt(ui_F_Data_Label, "%.3f", hlw8032_get_data(FACTOR));
        lv_label_set_text_fmt(ui_PF_C_Data_Label, "%.0f", hlw8032_get_data(PF_COUNT));
        lv_label_set_text_fmt(ui_One_Pulses_Data_Label, "%.0f", hlw8032_get_data(ONE_PULSES));
        lv_label_set_text_fmt(ui_Used_Data_Label, "%.6f Kw/h", hlw8032_get_data(EFFECTIVE_CALCULATION));
        lv_label_set_text_fmt(ui_Home_HLW_Label, "ACV:   %7.3f V    AC: %6.3f A\nPow:   %6.3f W    \nUsed: %7.5f Kw/h", hlw8032_get_data(EFFECTIVE_VOLTAGE), hlw8032_get_data(EFFECTIVE_CURRENT), hlw8032_get_data(ACTIVE_POWER), hlw8032_get_data(EFFECTIVE_CALCULATION));
        if (language_state.EN)
        {
            lv_label_set_text_fmt(ui_ElectricalOpenNumLabel, "ElectricalOpenNum          ---                                           %d", ch423_get_open_num_data());
        }
        else
        {
            lv_label_set_text_fmt(ui_ElectricalOpenNumLabel, "开启用电器数量             ---                                           %d", ch423_get_open_num_data());
        }
    }
    /* update hlw8032 screen data end */

    /* update switch status start */
    if (time_count.lv_time1 % 5 == 0)
    {
        update_switch_ui_state(ch423_get_io_data());
    }
    /* update switch status end */

    /* update sys running time and wifi status start */
    if (time_count.lv_time1 % 10 == 0)
    {
        lv_label_set_text_fmt(ui_AboutRunningTimeDataLabel, "%s", esp_log_system_timestamp());
        if (wifi_panel().authmode != WIFI_AUTH_OPEN)
        {
            lv_obj_clear_flag(ui_WIFI_On_Image, LV_OBJ_FLAG_HIDDEN); /* display wifi img */
        }
        else
        {
            lv_obj_add_flag(ui_WIFI_On_Image, LV_OBJ_FLAG_HIDDEN); /* hidden wifi img */
        }
    }
    /* update sys running time and wifi status end */

    /* update max30102 data start */
    if (time_count.lv_time1 % 10 == 0)
    {
        lv_label_set_text_fmt(ui_Heart_Spo2_Panel_Label, "MASTER\n\nHR : \n%d p/m\n\nSpO2 : \n%d %%", esp_now_get_max30102_data(HR), esp_now_get_max30102_data(SOP2));
    }
    /* update max30102 data end */

    /* update room environment data start */
    if (time_count.lv_time1 % 10 == 0)
    {
        lv_label_set_text_fmt(ui_ENVLabel, "%.0fC°       %.0f%%         Y", aht_get_data(TEMPERATURE), aht_get_data(HUMIDITY));
        lv_label_set_text_fmt(ui_WeatherLabel, "Env %.1fC°", aht_get_data(TEMPERATURE));
    }
    /* update room environment data end */

    /* update wifi channel start */
    if (time_count.lv_time1 % 30 == 0)
    {
        sprintf(str_dis, "mqtt push ok!\nCHANNEL >> %d", esp_now_get_channel());
        lv_roller_set_options(ui_MsgRoller, str_dis, LV_ROLLER_MODE_NORMAL);
        lv_roller_set_selected(ui_MsgRoller, 1, LV_ANIM_ON);
    }
    else if (time_count.lv_time1 % 15 == 0)
    {
        lv_roller_set_selected(ui_MsgRoller, 0, LV_ANIM_ON);
    }
    /* update wifi channel end */

    /* update brightness start */
    set_brightness_last_now = lv_slider_get_value(ui_Brightness_Slider);
    if (set_brightness_last_now != set_brightness_last)
    {
        set_brightness_last = set_brightness_last_now;
        ESP_LOGI(TAG, "brightness:%d %%", set_brightness_last_now);
        lv_label_set_text_fmt(ui_Brightness_Label, "%d %%", set_brightness_last_now);
        ledc_set_brightness(lv_slider_get_value(ui_Brightness_Slider));
    }
    /* update brightness end */
}

/**
 * @brief lv_add_timer
 *
 */
void lv_add_timer(void)
{
    /* add lv_timer start */
    updata_task1 = lv_timer_create(updata_task1_cb, 100, 0); /* T = 100ms */
    /* add lv_timer end */
}

/**
 * @brief lv_app_create_cb
 *
 */
void lv_app_create_cb(void)
{
    /* create wifi switch&&button cb event start */
    lv_obj_add_event_cb(ui_Wifi_Switch, connect_wifi_switch_event_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(ui_Wifi_Ensure_Button, connect_wifi_button_event_handler, LV_EVENT_ALL, NULL);
    /* create wifi switch&&button cb event end */

    /* create language switch cb event start */
    lv_obj_add_event_cb(ui_LanguageSwitch, language_event_handler, LV_EVENT_ALL, NULL);
    /* create language switch cb event end */

    /* init switch button obj state start */
    update_switch_ui_state(ch423_get_io_data());
    /* init switch button obj state end */

    /* create relay switch cb event start */
    lv_obj_add_event_cb(ui_Living_Light_Switch, relay_event_handler, LV_EVENT_ALL, NULL); /* bit0 */
    lv_obj_add_event_cb(ui_Living_Fan_Switch, relay_event_handler, LV_EVENT_ALL, NULL);   /* bit1 */
    lv_obj_add_event_cb(ui_Air_C_Switch, relay_event_handler, LV_EVENT_ALL, NULL);        /* bit2 */
    lv_obj_add_event_cb(ui_Living_TV_Switch, relay_event_handler, LV_EVENT_ALL, NULL);    /* bit3 */
    lv_obj_add_event_cb(ui_Living_Air_P_Switch, relay_event_handler, LV_EVENT_ALL, NULL); /* bit4 */
    lv_obj_add_event_cb(ui_Bed_Light_Switch, relay_event_handler, LV_EVENT_ALL, NULL);    /* bit5 */
    lv_obj_add_event_cb(ui_Bed_Fan_Switch, relay_event_handler, LV_EVENT_ALL, NULL);      /* bit6 */
    lv_obj_add_event_cb(ui_Bed_A_C_Switch, relay_event_handler, LV_EVENT_ALL, NULL);      /* bit7 */
    /* create relay switch cb event end */

    /* create set time cb event start */
    lv_obj_add_event_cb(ui_Set_Time_Button, set_time_event_handler, LV_EVENT_ALL, NULL); /* set time button */
    /* create set time cb event end */

    /* create on_all and close_all button start */
    lv_obj_add_event_cb(ui_ON_ALL_Button, on_close_button_handler, LV_EVENT_ALL, NULL);    /* set time button */
    lv_obj_add_event_cb(ui_All_Close_Button, on_close_button_handler, LV_EVENT_ALL, NULL); /* set time button */
    /* create on_all and close_all button end */
}
